// 2018版本之后 不用这个了。直接use即可。
// extern crate piston_window;

use piston_window::*;
use pingpang::racket::*;
use pingpang::ball::*;
use pingpang::base::*;

// 在界面上绘画
fn draw_thing(ctx: Context, graph: &mut impl Graphics, racket :&PingPangRacket, ball: &Ball) {
    // 清屏
    clear([1.0; 4], graph);
    // 绘制球拍
    racket.draw(ctx, graph);
    // 绘制球
    ball.draw(ctx, graph);
}

fn main() {
    // 1、创建一个窗口, 标题:乒乓球 大小:640*480 exit_on_esc：按esc键退出 
    let mut window: PistonWindow = WindowSettings::new("乒乓球", [WINDOW_WIDTH, WINDOW_HEIGHT + 20]).exit_on_esc(true).build().unwrap();
    // 初始化一个球拍
    let mut racket = PingPangRacket::init();
    let mut ball = Ball::init();

    let _noting_do = || {};

    while let Some(event) = window.next() {
        // match event.press_args() {
        //     Some(Button::Keyboard(Key::Left))       =>  racket.move_left(),
        //     Some(Button::Keyboard(Key::Right))      =>  racket.move_right(),
        //     _                                       =>  noting_do()
        // };
        // if key_press == "left" || key_press == "right" {
        //     println!("{}", key_press);
        // }

        // if let Some(args) = event.button_args() {
        //     println!("Scancode {:?}", args.scancode);
        // }

        // if let Some(args) = event.render_args() {
        //     println!("Render {}", args.ext_dt);
        // }

        event.mouse_cursor(|pos| {
            racket.move_to(pos[0], pos[1]);
        });
        
        ball.move_to(&racket);

        window.draw_2d(&event, |context, graphics, _device| {draw_thing(context, graphics, &racket, &ball)});
    }
}